﻿using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChurchDiary.DAL.DAL
{
    public class DataAccess
    {
        #region constructor
        public DataAccess()
        {
            _defaultDB = null;
            _connectionString = string.Empty;
            try
            {
                bool IsLocal = System.Web.HttpContext.Current.Request.IsLocal;
                if (IsLocal == true)
                {
                    _connectionString = String.Format(ConfigurationManager.ConnectionStrings["ChurchDiaryServer"].ConnectionString);
                }
                else
                {
                    _connectionString = String.Format(ConfigurationManager.ConnectionStrings["ChurchDiaryServer"].ConnectionString);
                }
                _defaultDB = new SqlDatabase(_connectionString);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public DataAccess(bool IsAdmin)
        {
            try
            {
                _connectionString = ConfigurationManager.ConnectionStrings["GreenConnection"].ConnectionString;
                _defaultDB = new SqlDatabase(_connectionString);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public DataAccess(string connectionString)
        {
            _defaultDB = null;
            _connectionString = connectionString;
            _defaultDB = new SqlDatabase(_connectionString);
        }
        #endregion
        #region attributes
        private string _connectionString;
        private Database _defaultDB;
        private DbConnection _dbConnection;
        private DbTransaction _dbTransaction;
        #endregion
        #region public propperty
        public Database SQLServer
        {
            get { return _defaultDB; }
            set { _defaultDB = value; }
        }
        public void Open()
        {
            _dbConnection = _defaultDB.CreateConnection();
            if (_dbConnection != null)
            {
                _dbConnection.Open();
            }
        }
        public void Close()
        {
            _dbConnection.Close();
            _dbConnection.Dispose();
            _dbConnection = null;
        }
        #endregion
        #region transaction functions
        public void BeginTransaction()
        {
            _dbConnection = _defaultDB.CreateConnection();
            if (_dbTransaction != null)
            {
                throw new Exception("Trying to create trnascation when one already exists.");
            } // end if
            if (_dbConnection != null)
            {
                _dbConnection.Open();
                _dbTransaction = _dbConnection.BeginTransaction();
            }
            else
            {
                throw new Exception("Failed to connect to database");
            } // end if
        }
        public void CommitTransaction()
        {
            if (_dbTransaction != null)
            {
                _dbTransaction.Commit();
                _dbTransaction.Dispose();
                _dbTransaction = null;
                _dbConnection.Close();
                _dbConnection.Dispose();
                _dbConnection = null;
            }
        }
        public void RollbackTransaction()
        {
            if (_dbTransaction != null && _dbTransaction.Connection != null)
            {
                _dbTransaction.Rollback();
                _dbTransaction.Dispose();
                _dbTransaction = null;
                _dbConnection.Close();
                _dbConnection.Dispose();
                _dbConnection = null;
            }
        }
        #endregion
    }
}
